package com.haoyuchanghong.com;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.FiniteTimeAction;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.actions.ease.EaseBounceIn;
import com.wiyun.engine.actions.grid.StopGrid;
import com.wiyun.engine.actions.grid.Twirl;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Director.IDirectorLifecycleListener;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.WYGLSurfaceView;
import com.wiyun.engine.types.WYGridSize;
import com.wiyun.engine.types.WYSize;

public class TestActivity extends Activity implements IDirectorLifecycleListener {
	protected WYGLSurfaceView mGLSurfaceView;
	private boolean mStarted = false;
	protected Scene mScene;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("crazy", "on create");
		// 通过程序方式创建content view, 也可以通过xml方式
		super.onCreate(savedInstanceState);
		getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		mGLSurfaceView = new WYGLSurfaceView(this);
		setContentView(mGLSurfaceView);

		// 设置显示帧率，程序发布时应该去掉
		Director.getInstance().setDisplayFPS(true);

		Director.getInstance().addLifecycleListener(this);
	}

	void showExitConfirm() {
		runOnUiThread(new Runnable() {
			public void run() {
				AlertDialog.Builder builder = new Builder(TestActivity.this);
				builder.setMessage("是否确认退出吗?").setNegativeButton("No", null)
						.setPositiveButton("Yes", new OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								finish();
							}
						}).show();
			}
		});
	}

	/**
	 * 检查这个demo是否需要一些前提条件
	 * 
	 * @return 如果前提条件满足, 返回null, 如果不满足, 返回一个错误信息
	 */
	protected String checkPrecondition() {
		return null;
	}

	@Override
	protected void onPause() {
		super.onPause();

		Director.getInstance().pause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		Director.getInstance().resume();
	}

	@Override
	protected void onDestroy() {
		Director.getInstance().end();

		super.onDestroy();
	}

	protected void createScene() {
		mScene = FirstScene.make();
		WYSize s = Director.getInstance().getWindowSize();
		Sprite backgroud = Sprite.make(R.drawable.backgroud);
		Sprite title = Sprite.make(R.drawable.title);
		backgroud.setPosition(s.width / 2, s.height / 2);
		title.setPosition(s.width / 2, s.height * 2 / 4);

		IntervalAction move = (IntervalAction) MoveTo.make(0.8f, 0 - s.width / 2, s.height * 2 / 4, s.width / 2,
				s.height * 2 / 4).autoRelease();
//		IntervalAction a = (IntervalAction) Twirl.make(1, WYGridSize.make(20, 30), title.getAnchorX()+title.getWidth()/2, title.getAnchorY()-title.getHeight()/2, -2.5f, 0.5f)
//		.autoRelease();
		IntervalAction a = (IntervalAction)Twirl.make(4, WYGridSize.make(20, 20),s.width / 2,s.height / 2, s.width / 2, s.height / 2, 2,2, 1).autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(move,EaseBounceIn.make(a), (FiniteTimeAction) StopGrid.make().autoRelease()).autoRelease();
		 Action repeat = (Action)RepeatForever.make(seq).autoRelease();
		title.runAction(repeat);

		mScene.addChild(backgroud);
		mScene.addChild(title);
		mScene.addChild(createLayer());

		backgroud.autoRelease(true);
		title.autoRelease(true);
		mScene.autoRelease(true);
	}

	protected Layer createLayer() {
		return new LayerFirst();
	}

	public void onSurfaceChanged(int w, int h) {
		/*
		 * 把第一个场景的创建放在这里, 可以避免一些屏幕尺寸问题. 因为一开始WiEngine并不知道 SurfaceView的尺寸,
		 * 所以是假设为全屏大小. 在之后的onSurfaceChanged事件中, 正确的大小会被设置.
		 * 但是往往在onSurfaceChanged之前, 场景就已经被创建了, 所以如果因为某种原因不是全屏大小,
		 * 那么之前创建的场景位置就有误了.
		 * 
		 * 由于onSurfaceChanged有可能被调用多次, 用一个标志来防止多次运行.
		 */
		Log.e("crazy", "on surface changed (w, h) ");
		if (!mStarted) {
			mStarted = true;
			createScene();
			Director.getInstance().runWithScene(mScene);
		}
	}

	public void onSurfaceCreated() {
		Log.e("crazy", "on surface created ..");
		final String error = checkPrecondition();
		if (!TextUtils.isEmpty(error)) {
			runOnUiThread(new Runnable() {
				public void run() {
					AlertDialog.Builder builder = new Builder(TestActivity.this);
					builder.setMessage(error).setNegativeButton("OK", new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					}).show();
				}
			});
		}
	}

	public void onSurfaceDestroyed() {
	}

	/**
	 * 如果用到了TextBox, 则需要这个方法. 这个方法保证WiEngine能收到back键事件
	 */
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
			return mGLSurfaceView.dispatchKeyEvent(event);
		else
			return super.dispatchKeyEvent(event);
	}

	@Override
	public void onDirectorEnded() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorResumed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorScreenCaptured(String arg0) {
		// TODO Auto-generated method stub

	}
}